<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>博文编辑</title>
  <link href="../../static/images/favicon.ico" th:href="@{/images/favicon.ico}" rel="icon" type="image/x-ico">
  <link rel="stylesheet" href="../../static/lib/semantic/dist/semantic.min.css" th:href="@{/lib/semantic/dist/semantic.min.css}">
  <link rel="stylesheet" href="../../static/lib/editormd/css/editormd.min.css" th:href="@{/lib/editormd/css/editormd.min.css}">
  <link rel="stylesheet" href="../../static/css/me.css" th:href="@{/css/me.css}">
</head>
<body class="admin-body">



  <!--导航-->
  <nav th:replace="admin/common :: navigate('blog')" class="ui inverted gird-header" >
    <div class="ui container">
      <div class="ui inverted secondary stackable menu">
        <h2 class="ui olive header item" style="font-family: STSong">管理后台</h2>
        <a href="#" th:href="@{/admin/blogs}" class="active m-item item m-mobile-hide"><i class="home icon"></i>博文管理</a>
        <a href="#" th:href="@{/admin/types}" class=" m-item item m-mobile-hide"><i class="clone outline icon"></i>分类管理</a>
        <a href="#" th:href="@{/admin/memorys}" class="m-item item m-mobile-hide"><i class="coffee icon"></i>流年记管理</a>
        <a href="#" th:href="@{/admin/friendlinks}" class=" m-item item m-mobile-hide"><i class="pencil alternate icon"></i>友链管理</a>
        <a href="#" th:href="@{/admin/pictures}" class=" m-item item m-mobile-hide"><i class="image icon"></i>相册管理</a>
        <a href="#" th:href="@{/admin/resources}" class="m-item item m-mobile-hide"><i class="graduation cap icon"></i>资源管理</a>
        <div class="right m-item m-mobile-hide menu">
          <div class="ui dropdown item">
            <div class="text">
              <img class="ui avatar image" src="/static/images/me.jpg" th:src="@{${session.user.avatar}}">
              <span th:text="${session.user.nickname}">Mr.Luo</span>
            </div>
            <i class="dropdown icon"></i>
            <div class="menu">
              <a href="#" th:href="@{/admin/logout}" class="item">注销</a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <a class="ui menu toggle black icon button m-right-top m-mobile-show">
      <i class="sidebar icon"></i>
    </a>
  </nav>
  <div class="m-padded-tb-hugex">
  </div>



  <!--中间内容-->
  <div  class="">
    <div class="ui container">
      <form id="blog-form" action="#" th:object="${blog}" th:action="@{/admin/blogs}" method="post" class="ui form">
        <input type="hidden" name="_method" value="PUT">
        <input type="hidden" name="published">
        <input type="hidden" name="id" th:value="*{id}">

        <div class="required field">
          <div class="ui left labeled input">
            <div class="ui selection compact teal basic dropdown label">
              <input type="hidden" value="1" name="copyright" th:value="*{copyright}" >
              <i class="dropdown icon"></i>
              <div class="text">原创</div>
              <div class="menu">
                <div class="item" data-value="1">原创</div>
                <div class="item" data-value="0">转载</div>
              </div>
            </div>
            <input type="text" name="title" placeholder="标题" th:value="*{title}">
          </div>
        </div>

        <div class="required field">
          <div class="" id="md-content" style="z-index: 1 !important;">
            <textarea placeholder="博客内容" name="content" style="display: none" th:text="*{content}"></textarea>
          </div>
        </div>

        <div class="two fields">
          <div class="required field">
            <div class="ui left labeled action input">
              <label class="ui compact teal basic label">分类</label>
              <div class="ui fluid selection dropdown">

                <input type="hidden" name="type.id" th:value="${blog.type.id}">

                <i class="dropdown icon"></i>
                <div class="default text">分类</div>
                <div class="menu">
                  <div th:each="type : ${typeList}" class="item" data-value="1" th:data-value="${type.id}" th:text="${type.name}">我的故事</div>
                </div>
              </div>
            </div>
          </div>

<!--          <div class="required field">-->
<!--            <div class="ui left labeled input">-->
<!--              <label class="ui teal basic label">首图</label>-->
<!--              <input type="text" name="firstPicture" th:value="*{firstPicture}" placeholder="首图引用地址">-->
<!--            </div>-->
<!--          </div>-->

          <div class="required field">
            <div class="ui left labeled input">
              <label class="ui teal basic label">首图</label>
              <!-- 上传按钮 -->
              <input type="file" name="pictureUrl" id="fileInput" style="display: none;" onchange="uploadImage()">
              <!-- 隐藏的input，用于存储服务器返回的图片URL -->
              <input type="hidden" name="firstPicture" id="pictureUrl">
              <button type="button" onclick="browseFile()">选择图片</button>
              <img id="previewImage" style="max-width: 100%; max-height: 150px; display: none;">
            </div>
          </div>

        </div>

        <div class="required field">
          <textarea name="description" placeholder="博客描述..." maxlength="200" th:text="*{description}"></textarea>
        </div>

        <div class="inline fields">
          <div class="field">
            <div class="ui checkbox">
              <input type="checkbox" id="recommend" name="recommend"  th:checked="*{recommend}" class="hidden">
              <label for="recommend">推荐</label>
            </div>
          </div>
          <div class="field">
            <div class="ui checkbox">
              <input type="checkbox" id="shareStatement" name="shareStatement" th:checked="*{shareStatement}" class="hidden">
              <label for="shareStatement">转载声明</label>
            </div>
          </div>
          <div class="field">
            <div class="ui checkbox">
              <input type="checkbox" id="appreciation" name="appreciation" th:checked="*{appreciation}" class="hidden">
              <label for="appreciation">赞赏</label>
            </div>
          </div>
          <div class="field">
            <div class="ui checkbox">
              <input type="checkbox" id="commentabled" name="commentabled" th:checked="*{commentabled}" class="hidden">
              <label for="commentabled">评论</label>
            </div>
          </div>
        </div>

        <div class="ui error message"></div>

        <div class="ui right aligned container">
          <button type="button" class="ui button" onclick="window.history.go(-1)" >返回</button>
          <button type="button" id="save-btn" class="ui secondary button">保存</button>
          <button type="button" id="publish-btn" class="ui teal button">发布</button>
        </div>

      </form>
    </div>
  </div>

  <br>
  <br>
  <!--底部栏-->
  <footer th:replace="admin/common :: footer"  class="ui inverted vertical segment m-padded-tb-massive m-opacity">
    <!--容器-->
    <div class="ui center aligned container">
      <div class="ui inverted divided stackable grid">
        <div class="four wide column">
          <div style="font-size: large;font-weight: bold" class="ui inverted m-text-thin m-text-spaced m-margin-top-max" >联系我</div>
          <!--<div id="newblog-container">-->
          <!--<div class="ui inverted link list" th:fragment="newblogList">-->
          <!--<a href="#" th:href="@{/blog/{id}(id=${blog.id})}" target="_blank" class="item m-text-thin" th:each="blog : ${newblogs}" th:text="${blog.title}">最新文章</a>-->
          <!--</div>-->
          <!--</div>-->
          <div class="ui inverted link list">
            <div href="#" class="m-text-thin">Email：1632373384@qq.com</div>
            <div href="#" class="m-text-thin">QQ：1632373384</div>
          </div>
        </div>

<!--        <div class="four wide column" >-->
<!--          <div class="ui inverted link list">-->
<!--            <div class="item">-->
<!--              &lt;!&ndash;微信二维码&ndash;&gt;-->
<!--              <div style="font-size: large;font-weight: bold" class="ui inverted m-text-thin m-text-spaced " >微信</div>-->
<!--              <img src="../../static/images/wechat.jpg" th:src="@{/images/wechat.jpg}"  class="ui m-margin-top rounded image" alt="" style="width: 110px">-->
<!--            </div>-->
<!--          </div>-->
<!--        </div>-->

        <div class="four wide column">
          <div class="ui inverted link list">
            <div class="item">
              <!--QQ二维码-->
              <div style="font-size: large;font-weight: bold" class="ui inverted m-text-thin m-text-spaced " >QQ</div>
              <img src="../../static/images/qq.jpg" th:src="@{/images/qq.jpg}" class="ui m-margin-top rounded image" alt="" style="width: 110px">
            </div>
          </div>
        </div>
        <!--博客运行时间统计-->
        <div class="four wide column">
          <div style="font-size: large;font-weight: bold" class="ui inverted  m-text-thin m-text-spaced m-margin-top">客栈信息</div>
          <!--<p id="htmer_time" class="item m-text-thin"></p>-->
          <div id="blog-message">
            <div class="ui inverted link list" style="align-content: center;margin-top: 10px" th:fragment="blogMessage">
              <div class="m-text-thin" style="text-align: left;margin-left: 75px;">
                文章总数： <h2 class="ui orange header m-inline-block m-margin-top-null" style="font-size:medium;" th:text="${blogTotal}"> 14 </h2> 篇
              </div>
              <div class="m-text-thin" style="text-align: left;margin-left: 75px">
                访问总数： <h2 class="ui orange header m-inline-block m-margin-top-null" style="font-size:medium;" th:text="${blogViewTotal}"> 14 </h2> 次
              </div>
              <div class="m-text-thin" style="text-align: left;margin-left: 75px">
                评论总数： <h2 class="ui orange header m-inline-block m-margin-top-null" style="font-size:medium;" th:text="${blogCommentTotal}"> 14 </h2> 条
              </div>
              <div class="m-text-thin" style="text-align: left;margin-left: 75px">
                留言总数： <h2 class="ui orange header m-inline-block m-margin-top-null" style="font-size:medium;" th:text="${blogMessageTotal}"> 14 </h2> 条
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="ui inverted section divider"></div>
      <div style="color: #F08047;margin-top: -18px" class="ui inverted m-text-thin m-text-spaced">我的客栈已营业：<span id="htmer_time" class="item m-text-thin"></span> (*๓´╰╯`๓)</div>
<!--      <a rel="nofollow" href="http://www.beian.miit.gov.cn" target="_blank">粤ICP备2022122083号-1</a>-->
    </div>
    </div>

  </footer>


  <script src="../../static/lib/jquery-3.2.1/jquery-3.2.1.min.js" th:src="@{/lib/jquery-3.2.1/jquery-3.2.1.min.js}"></script>
  <script src="../../static/lib/semantic/dist/semantic.min.js" th:src="@{/lib/semantic/dist/semantic.min.js}"></script>
  <script src="../../static/lib/editormd/editormd.min.js" th:src="@{/lib/editormd/editormd.min.js}"></script>


  <script>


    // 浏览文件按钮点击事件
    function browseFile() {
      document.getElementById('fileInput').click();
    }

    // 图片预览和上传
    function uploadImage() {
      var input = document.getElementById('fileInput');
      var preview = document.getElementById('previewImage');

      // 检查是否选择了文件
      if (input.files && input.files[0]) {
        var reader = new FileReader();

        // 预览图片
        reader.onload = function(e) {
          preview.src = e.target.result;
          preview.style.display = 'block';
        };
        reader.readAsDataURL(input.files[0]);

        // 使用Ajax上传图片到后端
        var formData = new FormData();
        formData.append('image', input.files[0]);

        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'http://47.108.xxx.xxx:8080/file/uploadImage', true);

        xhr.onreadystatechange = function() {
          console.log("xhr.readyState " + xhr.readyState);
          console.log("xhr.status " + xhr.status);

          if (xhr.readyState === 4 && xhr.status === 200) {
            // 上传成功，处理后端返回的数据
            console.log('Response from server:', xhr.responseText);
            var responseData = JSON.parse(xhr.responseText);

            // 将图片地址赋值给firstPicture
            var hiddenInput = document.getElementsByName('firstPicture')[0];
            if (hiddenInput) {
              hiddenInput.value = 'http://'+responseData.imageUrl;
            } else {
              console.error('无法找到名为firstPicture的隐藏input元素');
            }

            console.log('上传成功:', responseData);
          } else if (xhr.readyState === 4 && xhr.status !== 200) {
            // 上传失败，处理错误信息
            console.error('上传失败:', xhr.statusText);
          }
        };
        xhr.send(formData);
      }
    }

      // $('#newblog-container').load(/*[[@{/footer/newblog}]]*/"/footer/newblog");


      $('#blog-message').load(/*[[@{/footer/blogmessage}]]*/"/footer/blogmessage");

      // 运行时间统计
      function secondToDate(second) {
          if (!second) {
              return 0;
          }
          var time = new Array(0, 0, 0, 0, 0);
          if (second >= 365 * 24 * 3600) {
              time[0] = parseInt(second / (365 * 24 * 3600));
              second %= 365 * 24 * 3600;
          }
          if (second >= 24 * 3600) {
              time[1] = parseInt(second / (24 * 3600));
              second %= 24 * 3600;
          }
          if (second >= 3600) {
              time[2] = parseInt(second / 3600);
              second %= 3600;
          }
          if (second >= 60) {
              time[3] = parseInt(second / 60);
              second %= 60;
          }
          if (second > 0) {
              time[4] = second;
          }
          return time;
      }
      function setTime() {
          /*此处为网站的创建时间*/
        var create_time = Math.round(new Date(Date.UTC(2023, 12, 9, 15, 15, 15)).getTime() / 1000);
          var timestamp = Math.round((new Date().getTime() + 8 * 60 * 60 * 1000) / 1000);
          currentTime = secondToDate((timestamp - create_time));
          currentTimeHtml = currentTime[0] + '年' + currentTime[1] + '天'
              + currentTime[2] + '时' + currentTime[3] + '分' + currentTime[4]
              + '秒';
          document.getElementById("htmer_time").innerHTML = currentTimeHtml;
      }
      setInterval(setTime, 1000);


    //初始化Markdown编辑器
    var contentEditor;
    $(function() {
      contentEditor = editormd("md-content", {
        width   : "100%",
        height  : 640,
        syncScrolling : "single",
//        path    : "../static/lib/editormd/lib/"
        path    : "/lib/editormd/lib/",


        // markdown : md-content,
        // codeFold : true,
        // //syncScrolling : false,
        // saveHTMLToTextarea : true,    // 保存 HTML 到 Textarea
        // searchReplace : true,
        // //watch : false,                // 关闭实时预览
        htmlDecode : "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
        // //toolbar  : false,             //关闭工具栏
        // //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
        // emoji : true,
        // taskList : true,
        tocm            : true,         // Using [TOCM]
        tex : true,                   // 开启科学公式TeX语言支持，默认关闭
        flowChart : true,             // 开启流程图支持，默认关闭
        sequenceDiagram : true,       // 开启时序/序列图支持，默认关闭,
        // //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
        // //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
        // //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
        // //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
        // //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff
        // imageUpload : true,
        // imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"]
      });
    });
    $('.menu.toggle').click(function () {
      $('.m-item').toggleClass('m-mobile-hide');
    });

    $('.ui.dropdown').dropdown({
      on : 'hover'
    });

    $('#save-btn').click(function () {
      $('[name="published"]').val(false);
      $('#blog-form').submit();
    });

    $('#publish-btn').click(function () {
      $('[name="published"]').val(true);
      $('#blog-form').submit();
    });



    $('.ui.form').form({
      fields : {
        title : {
          identifier: 'title',
          rules: [{
            type : 'empty',
            prompt: '标题：请输入博客标题'
          }]
        },
        content : {
          identifier: 'content',
          rules: [{
            type : 'empty',
            prompt: '标题：请输入博客内容'
          }]
        },
        typeId : {
          identifier: 'type.id',
          rules: [{
            type : 'empty',
            prompt: '标题：请输入博客分类'
          }]
        },
        // firstPicture : {
        //   identifier: 'firstPicture',
        //   rules: [{
        //     type : 'empty',
        //     prompt: '标题：请输入博客首图'
        //   }]
        // },
        description : {
          identifier: 'description',
          rules: [{
            type : 'empty',
            prompt: '标题：请输入博客描述'
          }]
        }
      }
    });

  </script>
</body>
</html>
